﻿using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using PortalWeb.Business.Entity;
using PortalWeb.Business.Manager;
using PortalWeb.Core.Entity;
using PortalWeb.Service;
using PortalWeb.I18N;

namespace PortalWeb.Models
{
    public class ManageUserViewModel
    {
        [Required]
        [DataType(DataType.Password)]
        [Display(ResourceType = typeof (Resources), Name = "OldPassword")]
        public string OldPassword { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "{0} 必须至少包含 {2} 个字符。", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(ResourceType = typeof (Resources), Name = "NewPassword")]
        public string NewPassword { get; set; }

        [DataType(DataType.Password)]
        [Display(ResourceType = typeof (Resources), Name = "ConfirmPassword")]
        [Compare("NewPassword", ErrorMessage = "新密码和确认密码不匹配。")]
        public string ConfirmPassword { get; set; }
    }

    public class SelectUserRolesViewModel
    {
        public SelectUserRolesViewModel()
        {
            this.Roles = new List<SelectRoleEditorViewModel>();
        }


        // Enable initialization with an instance of ApplicationUser:
        public SelectUserRolesViewModel(ApplicationRoleManager roleManager,ApplicationUserModel user)
            : this()
        {
            this.UserName = user.UserName;
            //this.FirstName = user.FirstName;
            //this.LastName = user.LastName;

            var allRoles = roleManager.Roles;
            foreach (var role in allRoles)
            {
                // An EditorViewModel will be used by Editor Template:
                var rvm = new SelectRoleEditorViewModel(role);
                this.Roles.Add(rvm);
            }
            // Set the Selected property to true for those roles for 
            // which the current user is a member:
            foreach (var userRole in user.Roles)
            {
                var checkUserRole =Roles.Find(r => r.RoleId == userRole.RoleId);
                if (checkUserRole!=null)
                {
                    checkUserRole.Selected = true;
                }
            }
        }

        public string UserName { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public List<SelectRoleEditorViewModel> Roles { get; set; }
    }

    // Used to display a single role with a checkbox, within a list structure:
    public class SelectRoleEditorViewModel
    {
        public SelectRoleEditorViewModel()
        {
        }

        public SelectRoleEditorViewModel(ApplicationRoleModel role)
        {
            this.RoleId = role.Id;
            this.RoleName = role.Name;
        }

        public bool Selected { get; set; }

        public string RoleId { get; set; }

        [Required]
        public string RoleName { get; set; }
    }

}
